$(function () {
    navSetting.initItem();
    navSetting.initCrumb();


    $("#sidebar  li a").click(function () {
        var id = $(this);
        navSetting.storeItem(id);
    });

    $(".submenu a").click(function () {
        menuEvent.toggle($(this));
    });


    $("body").show();
});

var navSetting = (function () {


    return {
        //初始化菜单项目
        initItem: function () {
            var leftMenu = getCookie("left-menu");
            console.log(leftMenu);
            var indexArray = JSON.parse(leftMenu);
            $("#sidebar li").removeClass("open");
            $("#sidebar li").removeClass("active");
            var index = 0;
            if (indexArray != null && indexArray.length >= 0) {
                index = indexArray.pop();
                if (index == null || typeof (index) == "undefined") {
                    index = 0;
                }
            }
            index = parseInt(index);
            var li_one = $("#sidebar > ul > li")[index];
            $(li_one).addClass("active");

            if (indexArray != null && indexArray.length > 0) {
                var index2 = indexArray.pop();
                if (index2 != null || typeof (index2) != "undefined") {
                    index2 = parseInt(index2);
                }
                var li_two = $($("#sidebar > ul > li")[index]).find("ul > li > a")[index2];
                console.log($(li_two).html())
                $(li_two).addClass("active");
                $(li_one).addClass("open");
            }

        },
        //存储菜单项目
        storeItem: function (id) {
            Array
            indexArr = new Array();

            Array
            crumbArray = new Array();

            var parent = $(id).parent().parent().parent();
            if ($(parent).attr("id") != "sidebar") {
                var index_nav_one = $(parent).index();
                var index_nav_two = $(id).index();
                indexArr.push(index_nav_two);
                indexArr.push(index_nav_one);

                var href_nav_one = "";
                var value_nav_one = $(parent).find("a > span").html();
                var href_nav_two = $(id).attr("href");
                var value_nav_two = $(id).find("span").html();

                var obj1 = crumbSetting.createObj(index_nav_one, href_nav_one, value_nav_one, "menu");
                var obj2 = crumbSetting.createObj(index_nav_two, href_nav_two, value_nav_two, "menu");
                crumbArray.push(obj1);
                crumbArray.push(obj2);

            } else {
                var index_nav_two = $(id).parent().index();
                indexArr.push(index_nav_two);
                var href_nav_two = $(id).attr("href");
                var value_nav_two = $(id).find("span").html();

                var obj2 = crumbSetting.createObj(index_nav_two, href_nav_two, value_nav_two, "menu");
                crumbArray.push(obj2);
            }
            var storeStr = JSON.stringify(indexArr);
            var crumbStr = JSON.stringify(crumbArray);
            setCookie("left-menu", storeStr);
            setCookie("crumb-array", crumbStr);
        },
        //初始化面包屑
        initCrumb: function () {
            var crumbArray = getCookie("crumb-array");
            crumbArray = JSON.parse(crumbArray);
            if (crumbArray == null || crumbArray.length == 0) {
                return;
            }
            var crumb = $("#breadcrumb");
            $.each(crumbArray, function (index, ele) {
                var a = $("<a>");
                $(a).attr("href", ele.href);
                $(a).html(ele.value);
                $(crumb).append(a);
            });

            $("#crumb_title").html(crumbArray[0].value);

            console.log(crumbArray);

        }

    }


})();


var crumbSetting = (function () {

    return {
        crumbClick: function (value) {
            var crumbArray = getCookie("crumb-array");
            if (crumbArray == null) {
                crumbArray = new Array();
            }
            crumbArray = JSON.parse(crumbArray);
            var del = true;
            for (var i = crumbArray.length - 1; i >= 0; i--) {
                var obj = crumbArray[i];
                if (obj.value == value) {
                    del = false;
                }
                if (del == true) {
                    crumbArray.pop();
                }
            }
            setCookie("crumb-array", JSON.stringify(crumbArray));
        },
        pushCrumb: function (id, href, value, type) {
            var crumbArray = getCookie("crumb-array");
            crumbArray = JSON.parse(crumbArray);
            if (crumbArray == null) {
                crumbArray = new Array();
            }
            var obj = crumbSetting.createObj(id, href, value, type);
            crumbArray.push(obj);
            setCookie("crumb-array", JSON.stringify(crumbArray));

        },
        //创建面包屑对象
        createObj: function (id, href, value, type) {

            var obj = new Object();
            obj.id = id;
            obj.href = href;
            obj.type = type;
            obj.value = value;
            return obj;
        }

    };

})();

var menuEvent = (function () {

    return {
        toggle: function (e) {
            var submenu = $(e).siblings('ul');
            var li = $(e).parents('li');
            var submenus = $('#sidebar li.submenu ul');
            var submenus_parents = $('#sidebar li.submenu');
            $("#sidebar li").removeClass("active");
            $(e).parent().addClass("active");
            if (li.hasClass('open')) {
                if (($(window).width() > 768) || ($(window).width() < 479)) {
                    submenu.slideUp();
                } else {
                    submenu.fadeOut(250);
                }
                li.removeClass('open');
            } else {
                if (($(window).width() > 768) || ($(window).width() < 479)) {
                    submenus.slideUp();
                    submenu.slideDown();
                } else {
                    submenus.fadeOut(250);
                    submenu.fadeIn(250);
                }
                submenus_parents.removeClass('open');
                li.addClass('open');
            }
        }
    }

})();